package com.library.statistic.service;

import com.library.borrow.service.BorrowServiceImpl;
import com.library.entity.BookClass;
import com.library.entity.Statistic;
import com.library.search.service.SearchServiceImpl;
import com.library.statistic.dao.StatisticDaoImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=false)
public class StatisticServiceImpl {
	
	@Resource
	private StatisticDaoImpl statisticDaoImpl;
	@Resource
	private BorrowServiceImpl borrowServiceImpl;
	@Resource
	private SearchServiceImpl searchServiceImpl;

	public void addStatistic(Statistic statistic){
		this.statisticDaoImpl.saveStatistic(statistic);
	}

    @Transactional(readOnly=true)
	public Statistic getStatisticById(Integer statisticId)
	{
		return this.statisticDaoImpl.getStatisticById(statisticId);
	}

	//精确查找
	@Transactional(readOnly=true)
	public List<Statistic> getStatisticsByParams(Map params)
	{
		return this.statisticDaoImpl.findStatisticsByProperty(params);
	}

	//精确查找
	@Transactional(readOnly=true)
	public Statistic getStatisticByParams(Map params)
	{
		return this.statisticDaoImpl.findStatisticByProperty(params);
	}


	public void updateStatistic(Statistic statistic){
		this.statisticDaoImpl.updateStatistic(statistic);
	}

	/*查询数量*/
	@Transactional(readOnly=true)
	public Long getStatisticCountByParams(Map paramsMap){
		return this.statisticDaoImpl.findCountByProperty(paramsMap);
	}

	/*删除指定评价记录*/
	public void deleteStatistic(int statisticId){
		Statistic statistic = this.getStatisticById(statisticId);
		this.statisticDaoImpl.deleteStatistic(statistic);
    }

    /*增加记录BookClass的总统计*/
    public void addBookClassStatistic(BookClass bookClass){
		Map paramsMap = new HashMap();
		int bookClassBorrowTime = 0;
		int bookClassSearchTime = 0;
		int sumBorrowTime =this.borrowServiceImpl.getBorrowsCountByParams(paramsMap) == null ? 1: this.borrowServiceImpl.getBorrowsCountByParams(paramsMap).intValue();
		int sumSearchTime = this.searchServiceImpl.getStaffSearchsCountByParams(paramsMap) == null ? 1 :this.searchServiceImpl.getStaffSearchsCountByParams(paramsMap).intValue();

		paramsMap.put("bookClass",bookClass);
		List<Statistic> statistics = this.statisticDaoImpl.findStatisticsByProperty(paramsMap);

		for(int i = 0;i < statistics.size();++i){
			bookClassBorrowTime += statistics.get(i).getBorrowTime();
			bookClassSearchTime += statistics.get(i).getSearchTime();
		}	//end for

		Statistic statistic = new Statistic();
		statistic.setBookClass(bookClass);
		statistic.setBorrowTime(bookClassBorrowTime);
		statistic.setSearchTime(bookClassSearchTime);

		Double percentage = (statistic.getBorrowTime() * 100 / sumBorrowTime) * 0.8 + (statistic.getSearchTime() * 100 / sumSearchTime) * 0.2;
		statistic.setPercentage(String.valueOf(percentage.intValue()));

		this.addStatistic(statistic);

	}

	public void deleteBookClassStatistic(BookClass bookClass){
		Map paramsMap = new HashMap();
		paramsMap.put("bookClass",bookClass);
		paramsMap.put("staff",null);

		Statistic statistic = this.getStatisticByParams(paramsMap);
		if(statistic != null){
			this.deleteStatistic(statistic.getStatisticId());
		}
	}

	//添加所有图书类别的总统计信息
	public void addBookClassStatistic(List<BookClass> bookClasses){
		BookClass bookClass = null;

		for(int i = 0;i < bookClasses.size();++i){
			bookClass = bookClasses.get(i);
			this.addBookClassStatistic(bookClass);
		}	//end for
	}

	//删除所有图书类别的总统计信息
	public void deleteBookClassStatistic(List<BookClass> bookClasses){
		BookClass bookClass = null;

		for(int i = 0;i < bookClasses.size();++i){
			bookClass = bookClasses.get(i);
			this.deleteBookClassStatistic(bookClass);
		}	//end for
	}

	//更新总统计信息
	public void updateAllBookClassStatistic(List<BookClass> bookClasses){
		deleteBookClassStatistic(bookClasses);
		addBookClassStatistic(bookClasses);
	}
}
